<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 产品管理
 */
namespace app\bestbao\controller;
use app\bestbao\model\BestbaoCategory;
use app\bestbao\model\BestbaoProduct;
use think\facade\Request;

class Product extends Common{


    public function initialize(){
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'产品管理','url'=>'javascript:;']]);
    }


    /**
     * 列表
     */
    public function index(){
        $view['lists'] = BestbaoProduct::where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->paginate(20); 
        return view()->assign($view);
    }
    
    /**
     * 添加
     */
    public function create(){
        if(request()->isAjax()){
            $data = [
                'code'              => $this->request->param('code/s'),
                'category_id'       => $this->request->param('category_id/d'),
                'title'             => $this->request->param('title/s'),
                'note'              => $this->request->param('note/s'),
                'images'            => $this->request->param('images/s'),
                'member_miniapp_id' => $this->member_miniapp_id,
            ];
            $validate = $this->validate($data,'Product.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  BestbaoProduct::edit($data);
            if($result){
                return enjson(200,'操作成功',['url'=>url('product/index')]);
            }else{
                return enjson(0);
            }
        }else{
            $view['category'] = BestbaoCategory::where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->paginate(20); 
            return view()->assign($view);
        }
    }

    //编辑
    public function edit(){
        if(request()->isAjax()){
            $data = [
                'id'          => $this->request->param('id/s'),
                'code'        => $this->request->param('code/s'),
                'category_id' => $this->request->param('category_id/d'),
                'title'       => $this->request->param('title/s'),
                'note'        => $this->request->param('note/s'),
                'images'      => $this->request->param('images/s'),
            ];
            $validate = $this->validate($data,'product.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  BestbaoProduct::edit($data);
            if($result){
                return enjson(200,'操作成功',['url'=>url('product/index')]);
            }else{
                return enjson(0);
            }
        }else{
            $view['info']     = BestbaoProduct::where(['id' => Request::param('id/d'),'member_miniapp_id' => $this->member_miniapp_id])->find();
            $view['category'] = BestbaoCategory::where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->paginate(20); 
            return view()->assign($view);
        }
    }

    /**
     * 排序
     */
    public function sort(){
        if(request()->isAjax()){
            $data = [
                'sort' => $this->request->param('sort/d'),
                'id'   => $this->request->param('id/d'),
            ];
            $validate = $this->validate($data,'product.sort');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = BestbaoProduct::update(['sort'=>$data['sort']],['id' => $data['id']]);
            if($result){
                return enjson(200);
            }else{
                return enjson(0);
            }
        }
    }

    //删除
    public function delete(int $id){
        $result = BestbaoProduct::destroy($id);
        if($result){
            return enjson(200);
        }else{
            return enjson(403,'删除失败');
        } 
    }
}